package com.pur.zsms.supplier.service.impl;

import java.math.BigDecimal;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.pur.zsms.common.exception.GenericException;
import com.pur.zsms.common.support.PageController;
import com.pur.zsms.common.support.PaginationSupport;
import com.pur.zsms.common.vo.CommonType;
import com.pur.zsms.ordercustommanage.po.OrderSupplierPo;
import com.pur.zsms.ordercustommanage.vo.OrderSupplierVo;
import com.pur.zsms.supplier.dao.SupplierOrderDao;
import com.pur.zsms.supplier.service.ISupplierOrderService;

@Service("supplierOrderService")
public class SupplierOrderServiceImpl implements ISupplierOrderService {
	String msg = "";

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	@Resource(name = "supplierOrderDao")
	private SupplierOrderDao supplierOrderDao;

	public SupplierOrderDao getSupplierOrderDao() {
		return supplierOrderDao;
	}

	public void setSupplierOrderDao(SupplierOrderDao supplierOrderDao) {
		this.supplierOrderDao = supplierOrderDao;
	}

	@Override
	public PaginationSupport listSupplierOrder(PageController pageController, OrderSupplierVo orderSupplierVo) throws GenericException {
		return supplierOrderDao.listSupplierOrder(pageController, orderSupplierVo);
	}

	@Override
	public PaginationSupport findSupCheck(PageController pageController, OrderSupplierVo orderSupplierVo) throws GenericException {
		return supplierOrderDao.findSupCheck(pageController, orderSupplierVo);
	}

	public List getOrderSupMaterReal(String ordId, String ordCustId, String ordSupId) throws GenericException {

		return supplierOrderDao.getOrderSupMaterReal(ordId, ordCustId, ordSupId);
	}

	public List getOrderSupMaterRealForInStore(String ordId, String ordCustId, String ordSupId) throws GenericException {
		return supplierOrderDao.getOrderSupMaterRealForInStore(ordId, ordCustId, ordSupId);

	}

	public List getOrderSupMaterNeed(String ordId, String ordCustId, String ordSupId) throws GenericException {

		return supplierOrderDao.getOrderSupMaterNeed(ordId, ordCustId, ordSupId);
	}

	@Override
	public OrderSupplierPo getOrderSup(String ordId, String ordCustId, String ordSupId) throws GenericException {
		return supplierOrderDao.getOrderSup(ordId, ordCustId, ordSupId);
	}

	@Override
	public OrderSupplierPo getOrderSupByOrdIdSupId(String ordId, String ordSupId) throws GenericException {
		return supplierOrderDao.getOrderSupByOrdIdSupId(ordId, ordSupId);
	}

	@Override
	public void sendmater(OrderSupplierPo orderSupplierPo) throws GenericException {
		orderSupplierPo.setOrdState(4);
		supplierOrderDao.updateOrderSupplierPo(orderSupplierPo);
	}

	public boolean renturnSendmater(OrderSupplierPo orderSupplierPo) throws GenericException {
		if (orderSupplierPo.getOrdState() == CommonType.ORD_SUP_SEND) {
			orderSupplierPo.setOrdState(CommonType.ORD_SUP_WAIT);
			supplierOrderDao.updateOrderSupplierPo(orderSupplierPo);
			return true;
		} else if (orderSupplierPo.getOrdState() == CommonType.ORD_SUP_WAIT) {
			msg = "已改变成功！不需要返回状态！";
		} else {
			msg = "客户已经收获，请用加入退货单！";
		}
		return false;
	}
	
	public boolean updateOrderDate(OrderSupplierPo orderSupplierPo) throws GenericException{
		if (orderSupplierPo.getOrdState() == CommonType.ORD_SUP_WAIT) {
			supplierOrderDao.updateOrderSupplierPo(orderSupplierPo);
			return true;
		} else  {
			msg = "订单状态不正确 请回退状态！";
		}
		return false;
	}


	public boolean reviewOrderSup(OrderSupplierPo orderSupplierPo) throws GenericException {
		if ((orderSupplierPo.getOrdState() == CommonType.ORD_SUP_SEND || orderSupplierPo.getOrdState() == CommonType.ORD_STATE_RECEIVING)
				&& orderSupplierPo.getOrdStateSup() == CommonType.ORD_STATE_SUP_REVIEW_NOT) {
			orderSupplierPo.setOrdStateSup(CommonType.ORD_STATE_SUP_REVIEW);
			supplierOrderDao.updateOrderSupplierPo(orderSupplierPo);
			return true;
		} else if (orderSupplierPo.getOrdStateSup() == CommonType.ORD_STATE_SUP_REVIEW) {
			msg = "订单已经审核过！";
		} else {
			msg = "状态不正确 ，只有发货后才能审核！";
		}
		return false;
	}

	// 对账成功
	public boolean reconciliationForSup(OrderSupplierPo orderSupplierPo) throws GenericException {
		if ((orderSupplierPo.getOrdState() == CommonType.ORD_SUP_SEND || orderSupplierPo.getOrdState() == CommonType.ORD_STATE_RECEIVING)
				&& orderSupplierPo.getOrdStateSup() == CommonType.ORD_STATE_SUP_REVIEW) {
			orderSupplierPo.setOrdState(CommonType.ORD_STATE_RECONCILIATION);
			orderSupplierPo.setOrdStateSup(CommonType.ORD_STATE_RECONCILIATION);
			supplierOrderDao.updateOrderSupplierPo(orderSupplierPo);
			return true;
		} else if (orderSupplierPo.getOrdStateSup() == CommonType.ORD_STATE_SUP_REVIEW) {
			msg = "订单已经审核过！";
		} else {
			msg = "状态不正确 ，只有审核后才能对账！";
		}
		return false;
	}

	public boolean clearingForSup(OrderSupplierPo orderSupplierPo) throws GenericException {
		if (orderSupplierPo.getOrdStateSup() == CommonType.ORD_STATE_RECONCILIATION) {
			orderSupplierPo.setOrdState(CommonType.ORD_STATE_CLEARING);
			orderSupplierPo.setOrdStateSup(CommonType.ORD_STATE_CLEARING);
			supplierOrderDao.updateOrderSupplierPo(orderSupplierPo);
			return true;
		} else {
			msg = "状态不正确 ，只有对账后才能结款！";
		}
		return false;
	}

	public void updateOrderSupPoState(OrderSupplierPo orderSupplierPo, int state) throws GenericException {
		orderSupplierPo.setOrdState(state);
		supplierOrderDao.updateOrderSupplierPo(orderSupplierPo);
	}

	public void updateOrderSupPoStateAndSetValueReal(OrderSupplierPo orderSupplierPo, int state, boolean unusual) throws GenericException {
		// 查询真实总价格 ---下
		BigDecimal valueReal = supplierOrderDao.computeValueReal(orderSupplierPo.getOrdId(), orderSupplierPo.getOrdSupId());
		// 查询真实总价格 ---上
		orderSupplierPo.setOrdValueReal(valueReal);
		orderSupplierPo.setOrdState(state);
		orderSupplierPo.setCommitDeliverState(1);
		if (!unusual) {
			orderSupplierPo.setOrdPaymentState(1);
		}
		supplierOrderDao.updateOrderSupplierPo(orderSupplierPo);
	}

}
